package com.example.websockettest.webSocket.回收站;

import com.example.websockettest.webSocket.MyTool.BodyData;
import com.example.websockettest.webSocket.MyTool.MyRole;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class aa {

    public String getResponse(String prompt) {

        try {
            // Set API endpoint URL
            URL url = new URL("https://api.openai.com/v1/engines/davinci/completions");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");

            // Set request headers
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Authorization", "Bearer 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");
            ArrayList<MyRole> myRoleList=new ArrayList<>();
            myRoleList.add(new MyRole("user",prompt));
            BodyData bodyData=new BodyData("gpt-3.5-turbo",0.7,200,1, myRoleList);
            // Set request body
            String jsonInputString ="";
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(jsonInputString);
            wr.flush();
            wr.close();

            // Send HTTP POST request
            int responseCode = con.getResponseCode();
            System.out.println("Sending 'POST' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            // Read response
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // Return response as string
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public String getResponse(ArrayList<MyRole> myRoleList) {

        try {
            // Set API endpoint URL
            URL url = new URL("https://api.openai.com/v1/engines/davinci/completions");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");

            // Set request headers
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Authorization", "Bearer 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");
            //ArrayList<MyRole> myRoleList=new ArrayList<>();
            //myRoleList.add(new MyRole("user",prompt));
            BodyData bodyData=new BodyData("gpt-3.5-turbo",0.7,200,1, myRoleList);
            // Set request body
            String jsonInputString ="";
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(jsonInputString);
            wr.flush();
            wr.close();

            // Send HTTP POST request
            int responseCode = con.getResponseCode();
            System.out.println("Sending 'POST' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            // Read response
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // Return response as string
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        aa client = new aa();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("You: ");
            String prompt = scanner.nextLine();
            String response = client.getResponse(prompt);
            System.out.println("AI: " + response);
        }
    }
}

